
package com.jf.cloud.platform.service.impl;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.jf.cloud.api.leaf.feign.SegmentFeignClient;
import com.jf.cloud.common.cache.constant.PlatformCacheNames;
import com.jf.cloud.common.constant.DistributedIdKey;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.platform.dto.SysAccessKeyDTO;
import com.jf.cloud.platform.mapper.SysAccessKeyMapper;
import com.jf.cloud.platform.model.SysAccessKey;
import com.jf.cloud.platform.service.SysAccessKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 系统密钥
 * @author zz
 */
@Service
public class SysAccessKeyServiceImpl implements SysAccessKeyService {

    @Autowired
    private SysAccessKeyMapper sysAccessKeyMapper;
    @Autowired
    private SegmentFeignClient segmentFeignClient;

    @Override
    public PageVO<SysAccessKey> page(PageDTO pageDTO, SysAccessKeyDTO sysAccessKeyDTO) {
        return PageUtil.doPage(pageDTO, () -> sysAccessKeyMapper.list(sysAccessKeyDTO));
    }

    @Override
    public SysAccessKey getBySysAccessKeyId(Long sysAccessKeyId) {
        return sysAccessKeyMapper.getBySysAccessKeyId(sysAccessKeyId);
    }

    @Override
    @CacheEvict(cacheNames = PlatformCacheNames.SYS_ACCESS_KEY, key = "#sysAccessKey.accessId")
    public void save(SysAccessKey sysAccessKey) {
        ServerResponseEntity<Long> response = segmentFeignClient.getSegmentId(DistributedIdKey.MALL4CLOUD_AUTHORIZATION);
        if (!response.isSuccess()) {
            throw new LuckException(response.getMsg());
        }
        sysAccessKey.setAccessId(String.valueOf(response.getData()));
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        sysAccessKey.setAccessKey(sha256.digestHex(sysAccessKey.getAccessId() + System.currentTimeMillis()));
        sysAccessKeyMapper.save(sysAccessKey);
    }

    @Override
    @CacheEvict(cacheNames = PlatformCacheNames.SYS_ACCESS_KEY, key = "#sysAccessKey.accessId")
    public void update(SysAccessKey sysAccessKey) {
        SysAccessKey accessKey = new SysAccessKey();
        accessKey.setSysAccessKeyId(sysAccessKey.getSysAccessKeyId());
        accessKey.setName(sysAccessKey.getName());
        accessKey.setRemark(sysAccessKey.getRemark());
        sysAccessKeyMapper.update(accessKey);
    }

    @Override
    public void deleteById(Long sysAccessKeyId) {
        sysAccessKeyMapper.deleteById(sysAccessKeyId);
    }

    @Override
    @CacheEvict(cacheNames = PlatformCacheNames.SYS_ACCESS_KEY, key = "#accessId")
    public void removeCacheById(String accessId) {}

    @Override
    public void resetAccessKey(Long sysAccessKeyId) {
        SysAccessKey sysAccessKey = sysAccessKeyMapper.getBySysAccessKeyId(sysAccessKeyId);
        if (Objects.isNull(sysAccessKey)) {
            throw new LuckException("此数据已被删除");
        }
        Digester sha256 = new Digester(DigestAlgorithm.SHA256);
        sysAccessKey.setAccessKey(sha256.digestHex(sysAccessKey.getAccessId() + System.currentTimeMillis()));
        sysAccessKeyMapper.update(sysAccessKey);
    }

    @Override
    @Cacheable(cacheNames = PlatformCacheNames.SYS_ACCESS_KEY, key = "#accessId")
    public SysAccessKey getByAccessId(String accessId) {
        SysAccessKey sysAccessKey = sysAccessKeyMapper.getByAccessId(accessId);
        if (Objects.isNull(sysAccessKey)) {
            return new SysAccessKey();
        }
        return sysAccessKey;
    }
}
